package com.example.admin.controller;

import com.example.admin.service.BlogService;
import com.example.xo.common.Result;
import com.example.xo.pojo.Blog;
import com.example.xo.vo.BlogVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 王同学
 */
@RestController
@Slf4j
@RequestMapping("blog")
public class BlogController {
    @Autowired
    private BlogService blogService;

    @PostMapping("add")
    public Result add(@RequestBody Blog blog){
        //标题不能为空，或者标题长度==0   trim去除两端的空格，中间的空格不去掉
        if (blog.getTitle()==null||blog.getTitle().trim().length()==0){
            log.info("标题错误{}",blog.getTitle());
            return Result.error();
        }

        //如果名字的长度大于50那么就给他结束掉
        if (blog.getTitle().length()>50){
            log.info("标题{}超过50个字符",blog.getTitle());
            //超过就失败
            return Result.error();
        }
        //分类为空
        if (blog.getCategoryId()==null||blog.getCategoryId().trim().length()==0){
            log.info("分类不可为空{}",blog.getCategoryId());
            return Result.error();
        }
        //作者不可为空
        if (blog.getAuthor()==null||blog.getAuthor().trim().length()==0){
            log.info("作者不可为空{}",blog.getAuthor());
            return Result.error();
        }
        //作者名字不可超过10
        if (blog.getAuthor().length()>10){
            log.info("作者名字不可超过10：{}",blog.getAuthor());
            return Result.error();
        }
        //内容不可为空
        if (blog.getContent()==null||blog.getContent().trim().length()==0){
            log.info("内容不可为空{}",blog.getContent());
            return Result.error();
        }
        //成功
        return Result.success(blogService.add(blog));
    }
    @GetMapping("findAll")
    public Result findAll(){
        List<BlogVO> list = blogService.findAll();
        return Result.success(list);
    }
    @GetMapping("findById/{id}")
    public Result findById(@PathVariable String id){
        return Result.success(blogService.findPoById(id));

    }

    @PutMapping()
    public Result update(@RequestBody Blog blog){
        if (blog.getTitle().length()>50){
            log.info("名字长度超过50{}",blog.getTitle());
            return Result.error();
        }
        boolean res = blogService.update(blog);
        return Result.success(res);
    }

    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable String id){
        log.info("删除了一个:{}",id);
        boolean rss = blogService.deleteById(id);

        return Result.success(rss);
    }
    
    
    
}
